﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SpaceGame.Engine;
using SpaceGame.Objects;
namespace SpaceGame
{
    public partial class MainForm : Form
    {
        private SpaceGameEngine _game = new SpaceGameEngine();

        private Dictionary<Asteroid, PictureBox> asteroids = new Dictionary<Asteroid, PictureBox>();

        Timer timer = new Timer();

        private bool isInGame = false;
        public MainForm()
        {
            InitializeComponent();
           
        }

        private void PaintGame()
        {
            LifePlayer1.Maximum = _game.Players[0].Life;
            LifePlayer2.Maximum = _game.Players[1].Life;

            player1_name.Text = _game.Players[0].Name;
            player2_name.Text = _game.Players[1].Name;
          
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            PaintGame();
        }

        private void player1_settings_Click(object sender, EventArgs e)
        {
            ShowPlayerSettings(0);
        }

        private void player2_settings_Click(object sender, EventArgs e)
        {
            ShowPlayerSettings(1);
        }

        private void ShowPlayerSettings(int playerNumber)
        {
            PlayerSettingsForm sett = new PlayerSettingsForm(_game.Players[playerNumber]);
            sett.Show();
            sett.FormClosed += new FormClosedEventHandler((s, a) => PaintGame());     
        }

        private void start_bt_Click(object sender, EventArgs e)
        {
            player1_settings.Visible = false;
            player2_settings.Visible = false;
            start_bt.Visible = false;

             Repaint();
             isInGame = true;

            _game.StartGame();

            timer.Interval = 100;
            timer.Tick += new EventHandler((ss,ee) => Repaint());
            timer.Start();

            this.BringToFront();
            this.Focus();
        }



        public void Repaint()
        {
            if (isInGame)
            {
                player1_img.Left = _game.Players[0].X + Width / 2 - player1_img.Width / 2;
                player1_img.Top = -_game.Players[0].Y + Height / 2 - player1_img.Height / 2;

                player2_img.Left = _game.Players[1].X + Width / 2 - player2_img.Width / 2; ;
                player2_img.Top = -_game.Players[1].Y + Height / 2 - player2_img.Height / 2; 


                //LifePlayer1.Left = _game.Players[0].X + Width / 2 - LifePlayer1.Width / 2;
                //LifePlayer1.Top = _game.Players[0].Y + Width / 2 - LifePlayer1.Height / 2;

                //LifePlayer2.Left = _game.Players[0].X + Width / 2 - LifePlayer2.Width / 2;
                //LifePlayer2.Top = _game.Players[0].Y + Width / 2 - LifePlayer2.Height / 2;

                LifePlayer1.Value = _game.Players[0].Life;
                LifePlayer2.Value = _game.Players[1].Life;

                foreach (var aster in _game.Asteroids)
                {
                    if (!asteroids.Keys.Contains(aster))
                    {
                        asteroids.Add(aster,new PictureBox() {Image = aster.Image});
                        Controls.Add(asteroids[aster]);
                    }
                }

                foreach (var aster in _game.Asteroids)
                {
                    asteroids[aster].Left = aster.X + Width / 2 - aster.Image.Width / 2;
                    asteroids[aster].Top = aster.Y + Height / 2 - aster.Image.Height / 2;
                }

            }

            

        }

        private void MainForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            _game.ProcessKeyPressed(e.KeyChar.ToString());      


                
                 
                }

            

            

        



        private void MainForm_Paint(object sender, PaintEventArgs e)
        {
            Repaint();
        }

        private void player1_img_Click(object sender, EventArgs e)
        {

        }

      
     

    
   

    }

}
